# -*- coding: utf-8 -*-
import json

from devicetest.api import Asserts
from devicetest.log.logger import print_info
from devicetest.aw.OpenHarmony import CommonOH, BluetoothHelper
from devicetest.core.test_case import TestCase, Step

from aw.common import common_setup, common_teardown
from aw.pom import ControlCenter, Setting


class PreTest_RK3568_Basic_Systemui_0900(TestCase, ControlCenter):
    # 预置条件：
    # 烧写目标版本后，正常进入桌面
    # 测试步骤：
    # 进入桌面，右上角下划进入控制中心，点击控制中心蓝牙按钮
    # 预期结果：
    # 点击或重复点击有效，与设置蓝牙同步变化，无异常

    def __init__(self, controllers):
        self.TAG = self.__class__.__name__
        TestCase.__init__(self, self.TAG, controllers)

    def setup(self):
        common_setup(self.Phone1)
        Step('预置条件2：关闭蓝牙')
        try:
            BluetoothHelper.closeBluetooth(self.Phone1)
        except:
            pass

    def process(self):
        Step('步骤1：进入桌面，右上角下划进入控制中心，点击控制中心蓝牙按钮')
        CommonOH.startAbility(self.Phone1, Setting.ability_name, Setting.bundle_name)
        CommonOH.wait(self.Phone1, 1)
        CommonOH.touchByText(self.Phone1, '蓝牙')
        status = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle')).get('checked')
        for i in range(3):
            Step('步骤1.{}：点击或重复点击蓝牙按钮'.format(i))
            CommonOH.swipe(self.Phone1, **self.pull_down)
            CommonOH.wait(self.Phone1, 1)
            Step('预期结果1.{}：与设置蓝牙同步变化，无异常'.format(i))
            CommonOH.checkIfTextExist(self.Phone1, '控制中心')
            CommonOH.click(self.Phone1, **self.bluetooth_switch)
            CommonOH.wait(self.Phone1, 2)
            CommonOH.swipe(self.Phone1, **self.pull_up)
            property_dict = json.loads(CommonOH.getWidgetProperties(self.Phone1, condition='type/Toggle'))
            new_status = property_dict.get('checked')
            Asserts.assert_not_equal(status, new_status)
            status = new_status

    def teardown(self):
        common_teardown(self.Phone1, Setting.bundle_name)
        Step('收尾1：右上角上划，收回控制中心')
        CommonOH.swipe(self.Phone1, **self.pull_up)
        Step('收尾2：关闭蓝牙')
        try:
            BluetoothHelper.closeBluetooth(self.Phone1)
        except:
            pass
